﻿using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.Net.Sockets;
using System.Net;

namespace SDCManager
{
    public class Server
    {
        private Thread MC_th;
        private Thread S_th;
        #region 属性
        /// <summary>
        /// 返回服务器状态
        /// </summary>
        public ServerState State { get; set; }
        /// <summary>
        /// 已通过验证的ID列表
        /// </summary>
        public List<string> CheckedID { get; set; }
        /// <summary>
        /// 设置/返回服务端文件的路径或文件名
        /// </summary>
        public string Path { get; set; }
        /// <summary>
        /// 返回服务器信息
        /// </summary>
        public string ServerInfo { get; set; }
        /// <summary>
        /// 返回服务器日志
        /// </summary>
        public string Log { get; set; }
        private Process MCServer { get; set; }
        #endregion
        /// <summary>
        /// 初始化服务器
        /// </summary>
        /// <param name="path">服务端文件的路径或文件名</param>
        public Server(string path)
        {
            Path = path;
            CheckedID = new List<string>();
            State = ServerState.Abort;
            ServerInfo = "";
            Log = "";
        }
        public void Start()
        {
            if (State == ServerState.Abort)
            {
                MC_th = new Thread(delegate()//定义运行MC服务器的线程
                  {
                      Process p = new Process();
                      p.StartInfo.FileName = "java";
                      p.StartInfo.Arguments = "-Xincgc -Xms1G -Xmx1G -XX:MaxPermSize=128M -jar " + Path;
                      p.StartInfo.UseShellExecute = false;
                      p.StartInfo.RedirectStandardError = true;
                      p.StartInfo.RedirectStandardInput = true;
                      p.StartInfo.RedirectStandardOutput = true;
                      p.StartInfo.CreateNoWindow = true;
                      p.OutputDataReceived += new DataReceivedEventHandler(delegate (object sender, DataReceivedEventArgs e)
                        {
                            string msg = e.Data;
                            ServerInfo += msg + "\n";
                            //此处加入解析代码
                        });
                      MCServer = p;
                      p.Start();
                      p.BeginOutputReadLine();
                  });
                S_th = new Thread(delegate ()//定义进行验证的线程
                  {
                      int port = 1203;
                      UdpClient udpclient = new UdpClient(port);
                      IPEndPoint ipep = new IPEndPoint(IPAddress.Any, port);
                      while (true)
                      {
                          byte[] bytes = udpclient.Receive(ref ipep);
                          string strRec = Encoding.GetEncoding("gb2312").GetString(bytes, 0, bytes.Length);
                          string id = Security.Desecurify(strRec, "1234");
                          CheckedID.Add(id);
                      }
                  });
                State = ServerState.Running;
                MC_th.Start();
                S_th.Start();
            }
        }
    }
    /// <summary>
    /// 服务器状态
    /// </summary>
    public enum ServerState
    {
        /// <summary>
        /// 正在运行
        /// </summary>
        Running=1,
        /// <summary>
        /// 未运行
        /// </summary>
        Abort=0
    }
    /// <summary>
    /// 加密模块
    /// </summary>
    class Security
    {
        /// <summary>
        /// 解密方法
        /// </summary>
        /// <param name="str">密文</param>
        /// <param name="pw">密钥</param>
        /// <returns></returns>
        public static string Desecurify(string str , string pw)
        {
            return str;
        }
    }
}
